package com.generator.aboutUser.log.service.impl;

import com.generator.aboutUser.log.dao.LogMapper;
import com.generator.aboutUser.log.pojo.Journal;
import com.generator.aboutUser.log.service.ILogService;
import com.generator.aboutUser.user.pojo.Users;
import com.generator.sysManage.logManage.pojo.Log;
import com.generator.util.LogCondition;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/01/01
 * 星期六 20:13
 */
@Service
public class LogService implements ILogService {

    @Autowired
    private LogMapper logMapper;

    public LogMapper getLogMapper() {
        return logMapper;
    }

    public void setLogMapper(LogMapper logMapper) {
        this.logMapper = logMapper;
    }

    /**
     * 根据用户ID查询上一次登录日志
     * @param user
     * @return
     */
    @Transactional
    @Override
    public Journal getLastLoginTime(Users user) {
        return logMapper.queryLoginTime(user);
    }

    /**
     * 添加用户登录日志
     * @param user
     * @return
     */
    @Transactional
    @Override
    public boolean addLoginLog(Users user) {
        return logMapper.addLoginLog(user);
    }

    /**
     * 分页拿出日志信息
     * @param logCondition
     * @param pageModel
     * @return
     */
    @Override
    public PageModel<Log> getLogsByCondition(LogCondition logCondition,PageModel<Log> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        List<Log> logList = logMapper.queryLogsByCondition(logCondition);


        PageInfo<Log> pageInfo = new PageInfo<Log>(logList);

        pageModel.setData(new ArrayList<Log>(logList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());
        return pageModel;
    }

    /**
     * 根据日志ID查找日志信息
     * @param log
     * @return
     */
    @Override
    public Log queryLogById(Log log) {
        return logMapper.queryLogById(log);
    }


}
